package android.tkb.activity.word;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.tkb.dao.word.DatabaseHelper;
import android.tkb.model.word.Words;
import android.tkb.util.word.Contants;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 测试的结果页面
 * @author Administrator
 *
 */
public class TestResultActivity extends Activity {
	
	private int correntNum =0;
	private int errorNum = 0;
	
	private String value = Contants.E ;//给定一个初使值
	
	private ListView lv ;
	private TextView tv ;
	private Button back ;
	
	private ArrayList<Map<String,Object>> data;
	private List<Words> result;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		 setTitle(R.string.test_result);
		 setContentView(R.layout.test_result);
		 
		 /* 从上一下物件接收内容+++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
		 Intent intent = getIntent();
		 Words words = (Words) intent.getSerializableExtra("words");
		 value = intent.getStringExtra("flag");//标符中英
		
		 result = words.getPrefeList();//取出目标物件
		 
		 lv = (ListView) findViewById(R.id.resultView);
	     data = loadData(result,value);
	     SimpleAdapter adapter  = new SimpleAdapter(this,data,R.layout.test_result_list,new String[]{"flag","name"},new int[]{R.id.testResultTv1,R.id.testResultTv2});
	     lv.setAdapter(adapter);
	     lv.setOnItemClickListener(new ListViewOnItemClickListener());
	      
	     tv= (TextView) findViewById(R.id.testTv1);
	     tv.setText("合计："+(correntNum+errorNum)+"'正确:"+correntNum+"'错误"+errorNum);
	     
	     back = (Button) findViewById(R.id.back);
	     if(Contants.E.equals(value)){
			 back.setText(R.string.word_test_en);
		 }else if(Contants.C.equals(value)){
			 back.setText(R.string.word_test_ch);
		 }
	     
		 back.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(TestResultActivity.this, EnOrChTestActivity.class);
				intent.putExtra("key", value);
				startActivity(intent);
				finish();
			}
			 
		 });
	}
	
	class ListViewOnItemClickListener implements OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			updateDate();//更新计数
			//转到另一下页面------------------------------------------------------------------
			Intent intent  = new Intent();
			intent.setClass(TestResultActivity.this, ShowWordMessageActivity.class);
			Words words3 = new Words();
			
			words3.setPrefeList(result);
			
			intent.putExtra("words", words3);
			intent.putExtra("index", position);//索引
			intent.putExtra("flag", value);
			
			startActivity(intent);
			finish();
		}
		
	}
	/**
	 * 更新数据
	 */
	public void updateDate(){
		DatabaseHelper mOpenHelper = new DatabaseHelper(TestResultActivity.this,DatabaseHelper.DB_NAME,DatabaseHelper.DB_VERSION);
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		for (int i = 0; i < result.size(); i++) {
			Words w = result.get(i);
			
			ContentValues values  = new ContentValues();
			String state = w.getTempState();
			
			if(Contants.V.equals(state)){
				values.put("right_times", w.getRightTimes()!=null ? w.getRightTimes() + 1 : 1);
			}else if(Contants.X.equals(state)){
				values.put("wrong_times", w.getWrongsTimes() !=null ? w.getWrongsTimes() + 1 : 1);
			}
			
			String col[]={w.getEn()};
			db.update("words", values, "en = ? ", col);
		}
		db.close();
		mOpenHelper.close();
		
	}
	//组装ListView所需的数据
    public ArrayList<Map<String,Object>> loadData(List<Words> wordList,String flag){
    	ArrayList<Map<String,Object>> coll = new ArrayList<Map<String,Object>>();
    	Map<String,Object> item ;
    
    	for (int i = 0; i < wordList.size(); i++) {
    		Words w = wordList.get(i);
    		item =  new HashMap<String,Object> ();
    		String state = w.getTempState();
    		if(Contants.V.equals(state)){
    			correntNum++;
    		}else if(Contants.X.equals(state)){
    			errorNum++;
    		}
    		
    		item.put("flag", state);
    		
    		if(Contants.E.equals(flag)){//处理是否是中或英显示
    			item.put("name", w.getEn());
    		}else if(Contants.C.equals(flag)){
    			item.put("name", w.getJp());
    		}
    		
    		coll.add(item);
		}
    	
    	return coll;
    }
}
